package com.dd.cloud.user.service.finance.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.user.entity.finance.FinanceRatioSet;
import com.dd.cloud.user.mapper.finance.FinanceRatioSetMapper;
import com.dd.cloud.user.req.finance.FinanceRatioFrom;
import com.dd.cloud.user.service.finance.IFinanceRatioSetService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 财务设置 服务实现类
 * </p>
 *
 * @author czg
 * @since 2023-05-15
 */
@Service
public class FinanceRatioSetServiceImpl extends ServiceImpl<FinanceRatioSetMapper, FinanceRatioSet> implements IFinanceRatioSetService {

    @Override
    public FinanceRatioSet getFinanceDetail() {
        LambdaQueryWrapper<FinanceRatioSet> wrapper = new LambdaQueryWrapper<>();
        wrapper.last("limit 1");
        FinanceRatioSet financeRatioSet = this.getOne(wrapper);
        FinanceRatioSet newFinanceRatioSet = new FinanceRatioSet();
        if (financeRatioSet != null) {
            BeanUtils.copyProperties(financeRatioSet, newFinanceRatioSet);
        }
        return newFinanceRatioSet;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addOrUpdateFinanceRatio(FinanceRatioFrom financeRatioFrom) {
        LambdaQueryWrapper<FinanceRatioSet> wrapper = new LambdaQueryWrapper<>();
        wrapper.last("limit 1");
        FinanceRatioSet financeRatioSet = this.getOne(wrapper);
        if (financeRatioSet == null) {
            FinanceRatioSet newFinanceRatioSet = new FinanceRatioSet();
            BeanUtils.copyProperties(financeRatioFrom, newFinanceRatioSet);
            newFinanceRatioSet.setCreateDate(LocalDateTime.now());
            this.save(newFinanceRatioSet);
        } else {
            BeanUtils.copyProperties(financeRatioFrom, financeRatioSet);
            this.updateById(financeRatioSet);
        }
    }


}
